package com.hcrazy.manchester.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.Forum;
import com.hcrazy.manchester.app.pojo.Moderator;
import com.hcrazy.manchester.app.services.ForumService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/forum")
public class ForumController {
	
	@Autowired
	private ForumService forumService;
	
	@RequestMapping(value="/forum",method=RequestMethod.GET)
	public void forum(){
		
	}
	
	/**
	 * 跳转到添加子分类页面
	 * 初始化数据
	 * @param sortParent
	 * @param model
	 */
	@RequestMapping(value="/addForum",method=RequestMethod.GET)
	public String addForum(@RequestParam int parentId,@RequestParam int type,Model model){
		//根据父分类id获取父分类对象
		try {
			Forum parentForum = new Forum();
			if(parentId == 0){
				parentForum.setForumId(0);
				parentForum.setName("无");
			}else{
				parentForum = forumService.getForumById(parentId);
			}
			model.addAttribute("parentForum",parentForum);
			Forum forum = new Forum();
			forum.setType(type);
			forum.setStatus(Constant.FORUM_STATUS_NORMAL);
			model.addAttribute("forum",forum);
			if(type == 0)
				return "/admin/forum/addForumSort";
			else
				return "/admin/forum/addForum";
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return "/admin/failed";
		}
		
	}
	
	/**
	 * 跳转到编辑分类页面
	 * @param sortId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editForum",method=RequestMethod.GET)
	public String editForum(@RequestParam int forumId,Model model){
		
		try {
			Forum forum = forumService.getForumById(forumId);
			addForum(forum.getParentId(),forum.getType(),model);
			
			model.addAttribute("forum",forum);
			if(forum.getType() == 0)
				return "/admin/forum/addForumSort";
			else
				return "/admin/forum/addForum";
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return "/admin/failed";
		}
		
	}
    
	/**
	 * 删除分类
	 * @param sortId
	 * @return
	 */
	@RequestMapping(value="/delForum",method=RequestMethod.GET)
	public String delForum(@RequestParam int forumId,Model model){
		try {
			String msg = forumService.delForum(forumId);
			if(null != msg){
				model.addAttribute("message", msg);
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/forum/forum";
	}
	
	/**
	 * 提交分类信息
	 * @param sort
	 * @return
	 */
	@RequestMapping(value="/forum",method=RequestMethod.POST)
    public String addOrUpdateSort(@ModelAttribute("form") Forum forum,
    		Model model){
		try {
			forumService.addOrUpdateForum(forum);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "/admin/forum/forum";
    }
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/forums",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<Forum> getAllForums(){
		
		try {
			return forumService.getAllForums();
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/******************************论坛版主管理*************************************/
	
	@RequestMapping(value="/addModerator")
	public void addModerator(@RequestParam int forumId,Model model){
		//获取本版块的版主列表
		try {
			List<Moderator> moderators = forumService.getModeratorsByForumId(forumId);
			model.addAttribute("moderators",moderators);
			//初始化数据
			Forum forum = forumService.getForumById(forumId);
			model.addAttribute("forum",forum);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	@RequestMapping("/delModerator")
	public String delModerator(@RequestParam int forumId,@RequestParam String username){
		try {
			forumService.delModerator(forumId, username);
			return "redirect:/admin/forum/addModerator?forumId="+forumId;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return "/admin/failed";
		}
	}
	
	@RequestMapping(value="/moderator",method=RequestMethod.POST)
	public String saveModerator(@ModelAttribute("form") Moderator moderator,Model model){
		try {
			ErrorMsg msg = forumService.addModerator(moderator);
			if(!msg.getCode().equals(Constant.SUCCESS)){
				model.addAttribute("message", msg.getMsg());
				return "/admin/failed";
			}
			return "redirect:/admin/forum/addModerator?forumId="+moderator.getForumId();
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return "/admin/failed";
		}
	}
}
